var express =require("express");
var router = express.Router();

var {Userinfo,Leave,Adviss,Approval} = require("../utils/model")
var {createToken,checkToken} = require("../utils/token");
const { json } = require("express");
router.get("/test",(req,res)=>{
    res.json({
        code:200,
        msg:"测试接口 成功 successful "
    })
})  

router.get("/gettoken",(req,res)=>{
    var token = createToken({})
    res.json({
        token:token,
        msg:"token success",
        type:1,
        code:200
    })
})

router.post("/login",(req,res)=>{
    var body = req.body
    Userinfo.findOne({
        mobile:body.mobile
    }).then(result=>{
        if(result){
            if(result.password == body.password){
                var token = createToken({
                    username:result.username,
                    mobile:result.mobile,
                    password:result.password,
                    roleid:result.roleid
                })
                res.json({
                    code:200,
                    type:1,
                    token,
                    msg:"登录成功"
                })
            }else{
                res.json({
                    code:200,
                    type:0,
                    result,
                    msg:"用户名或密码不正确"
                })
            }
          
        }else{
            res.json({
                code:200,
                type:0,
                result,
                msg:"用户名或密码不正确"
            }) 
        }
        
    })
})

router.post("/regist",(req,res)=>{
    var body = req.body
    Userinfo.findOne({
        $or:[
            {username:body.username},
            {mobile:body.mobile}
        ]
    }).then(result=>{
        if(result){
            console.log(result)
            if(result.username == body.username){
                res.json({
                    code:200,
                    type:0,
                    msg:"用户名被占用"
                })
            }else{
                res.json({
                    code:200,
                    type:0,
                    msg:"手机号被占用"
                }) 
            }
        }else{
            body.roleid = "1"
            body.time = new Date()
            Userinfo.insertMany(body)
            .then(result=>{
                res.json({
                    code:200,
                    type:1,
                    msg:"注册成功"
                }) 
            })
        }
    })
})


// 签到
router.post("/sign",(req,res)=>{
    checkToken(req,res,({mobile})=>{
        var times = new Date()
        var show = null
        // times.setHours("8")
        var h = times.getHours()
        if(h<9){
            show = true
        }else{
            show = false
        }
        Userinfo.findOne({
            mobile
        }).then(result=>{
            Userinfo.updateOne({
                mobile
            },{
                $set:{
                    times,
                    show
                }
            }).then(result=>{
                res.json({
                    code:200,
                    type:1,
                    msg:"签到成功"
                }) 
            })
        })
    })
})

router.post("/user",(req,res)=>{
    Userinfo.find({
    
    }).then(result=>{
        if(result){
            res.json({
                code:200,
                type:1,
                msg:"查询成功",
                result
            })
        }
    })
})

router.post("/user1",(req,res)=>{
    var body = req.body
    Userinfo.find({
        username:"/^" + body.username + "/"
    }).then(result=>{
        if(result){
            res.json({
                code:200,
                type:1,
                msg:"查询成功",
                result
            })
        }
    })
})

router.post("/operation",(req,res)=>{
    var body = req.body
    Userinfo.findOne({
        $or:[
            {_id:body.id},
            {mobile:body.mobile}
        ]
    }).then(result=>{
        res.json({
            code:200,
            type:1,
            msg:"查询成功",
            result
        })
    })
})

//修改员工数据
router.post("/updateoper",(req,res)=>{
    var body = req.body
    Userinfo.updateMany({
        _id:body.id
    },{
        $set:{
            body
        }
    }).then(result=>{
        if(result){
            res.json({
                code:200,
                type:1,
                msg:"修改成功",
            })
        }
    })
})

router.post("/deloper",(req,res)=>{
    var body = req.body
    Userinfo.deleteOne({
        _id:body.id
    }).then(result=>{
        if(result){
            res.json({
                code:200,
                type:1,
                msg:"删除成功",
            })
        }
    })
})

//修改密码
router.post("/modify",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        var body = req.body
        Userinfo.findOne({
            mobile,
        }).then(result=>{
            if(result){
                if(result.password == body.pssd){
                    Userinfo.updateOne({
                        mobile
                    },{
                        $set:{
                            password:body.password
                        }
                    }).then(result=>{
                        if(result){
                            res.json({
                                code:200,
                                type:1,
                                msg:"修改成功",
                            })
                        }
                    })
                }else{
                    res.json({
                        code:500,
                        type:0,
                        msg:"旧密码错误",
                    })
                }
              
            }
        })
    })
})

// 申请请假
router.post("/leave",(req,res)=>{
    var body = req.body
    Leave.insertMany(body)
    .then(result=>{
        if(result){
            res.json({
                code:200,
                type:1,
                msg:"添加成功",
            })
        }
    })
})

// 查看请假
router.post("/findleave",(req,res)=>{
    Leave.find({})
    .then(result=>{
        if(result){
            res.json({
                code:200,
                type:1,
                msg:"查看成功",
                result,
            })
        }
    })
})

// 修改请假
router.post("/leaveupdate",(req,res)=>{
    var body = req.body
    Leave.updateMany({
        _id:body._id
    },{
        $set:{
            ...body
        }
    })
    .then(result=>{
        if(result){
            res.json({
                code:200,
                type:1,
                msg:"修改成功",
            })
        }
    })
})


router.post("/find",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
    Leave.find({
        mobile
    })
    .then(result=>{
        if(result){
            res.json({
                code:200,
                type:1,
                msg:"查看成功",
                result,
            })
        }
    })
    })
})

router.post("/gethomebanner",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        res.json({
            msg:"获取数据成功",
            code:200,
            type:1,
            result:[
                {
                    imgUrl:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599990426054&di=05688c8b18eebb4c5db0ee453755cef8&imgtype=0&src=http%3A%2F%2Fybtx.sznews.com%2Fimages%2Fattachement%2Fjpg%2Fsite231%2F20100209%2F001e4f9d18ab0cdb18e22d.jpg",
                    text:""
                },
                {
                    imgUrl:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599990756164&di=4852f4cb7f7515431fd2a31b0f7a8d79&imgtype=0&src=http%3A%2F%2Fyouimg1.c-ctrip.com%2Ftarget%2Ftg%2F773%2F779%2F902%2F7d4fef4bb2d1423d9b0b3f13566342be.jpg",
                    text:""
                },
                {
                    imgUrl:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599990828017&di=b3834b4de1c7b2cd658995564bdd0c96&imgtype=0&src=http%3A%2F%2Fpic.sucaibar.com%2Fpic%2F201306%2F28%2F2c49605247.jpg",
                    text:""
                },
                {
                    imgUrl:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599991863145&di=5e55d16df3b954b15c243970fa336ed5&imgtype=0&src=http%3A%2F%2Fp2.qhimgs4.com%2Ft01413bb306f3178805.jpg",
                    text:""
                }
            ]
        })
    })
}) 


// 审批请假
router.post("/approval",(req,res)=>{
    var body = req.body
    Leave.updateOne({
        _id:body.id
    },{
        $set:{
            flag:body.flag
        }
    })
    .then(result=>{
        if(result){
            res.json({
                code:200,
                type:1,
                msg:"审批成功",
            })
        }
    })
})


// 提交意见
router.post("/addadvise",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Userinfo.findOne({
            mobile,
        }).then(userinfo=>{
            Adviss.insertMany({
                time:new Date(),
                mobile,
                username:userinfo.username,
                reply:[],
                ...req.body
            }).then(result=>{
                res.json({
                    code:200,
                    msg:"添加成功",
                    result,
                    type:1
                })    
            }).catch(err=>{
                res.json({
                    code:500,
                    msg:"服务器异常",
                    err,
                    type:0
                })
            })
        })
    })
})


// 获取意见列表  
router.post("/getadviselist",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{ 
        var obj = {}
        Adviss.find(obj,{})
        .sort({_id:-1})
        .then(result=>{
            res.json({
                code:200,
                msg:"意见查询成功",
                result,
                type:1
            })    
        }).catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                err,
                type:0
            })
        })
    })
})

// 根据_id 删除意见列表 
router.post("/delAdviseOne",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Adviss.remove({
            _id:req.body._id
        })
        .then(result=>{
            res.json({
                code:200,
                msg:"意见删除成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                err,
                type:0
            })
        })
    })
}) 


// 获取意见 
router.post("/getadvisebyid",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{ 
        var obj = {
            _id:req.body._id
        }
        Adviss.findOne(obj,{})
        .then(result=>{
            res.json({
                code:200,
                msg:"查询成功",
                result,
                type:1
            })    
        }).catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                err,
                type:0
            })
        })
    })
})

// 修改意见 
router.post("/updateadvisebyid",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{ 
        var obj = {
            _id:req.body._id
        }
        Adviss.updateMany(obj,{
            $set:{
                time:new Date(),
                title:req.body.title,
                content:req.body.content,
                category:req.body.category
            }
        })
        .then(result=>{
            res.json({
                code:200,
                msg:"修改成功",
                result,
                type:1
            })    
        }).catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                err,
                type:0
            })
        })
    })
})


// 提交意见回复


router.post("/addreply",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{ 
        var {_id,username,tips} = req.body;
        Adviss.findOne({
            _id
        }).then(data=>{
            var reply = data.reply || [];
            console.log(reply);
            reply.push({
                author:username,
                mobile,
                roleid,
                tips,
                time:new Date(),
                id:reply.length
            })

            Adviss.updateMany({
                _id,
            },{
                $set:{
                    reply
                }
            }).then(result=>{
                res.json({
                    code:200,
                    msg:"回复成功",
                    result,
                    type:1
                })    
            }).catch(err=>{
                res.json({
                    code:500,
                    msg:"服务器异常",
                    err,
                    type:0
                })
            })
        })
    })
})

// 上传头像

var multer = require("multer");
var storage = multer.diskStorage({
    destination: function (req, file, cb) {   // 头像存放的目录 
        cb(null, './public/avatar')    // . 文件路径
    },
    filename: function (req, file, cb) {  // 新的文件名 
        cb(null, Date.now()+"WH2004"+file.originalname);  //  originalname 原文件名 
    }
})

var upload  =  multer({ storage: storage }).any();  // 可以上传任何格式的图片


router.post("/uploadpic",upload,(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
       
        if(req.files){
            var avatar = req.files[0].path;  // 头像的服务器地址 
            Userinfo.updateMany({
                mobile
            },{
                $set:{
                    avatar
                }
            }).then(result=>{
                res.json({
                    code:200,
                    msg:"头像上传成功",
                    type:1,
                    avatar
                })
            }).catch(err=>{
                res.json({
                    code:500,
                    msg:"服务器异常",
                    type:0
                })
            })
            
        }else{
            res.json({
                code:200,
                msg:"头像上传失败",
                type:0
            })
        }
    })
})

module.exports = router;